package com.taotao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.taotao.common.resp.EasyUITreeNode;
import com.taotao.common.resp.TaotaoResult;
import com.taotao.dao.ContentCategoryMapper;
import com.taotao.pojo.ContentCategory;
import com.taotao.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 *
 * Created by zzb on 2017/11/9.
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private ContentCategoryMapper contentCategoryMapper;


    public List<EasyUITreeNode> getContentCategoryList(long parentId) {
        //根据parentId获取ContentCategory List
        //将ContentCategory List 封装为EasyUiTreeNode List
        List<ContentCategory> contentCategories = contentCategoryMapper.findByParentId(parentId);

        List<EasyUITreeNode> nodes = new ArrayList<EasyUITreeNode>();
        if(contentCategories.size() != 0) {
            for(ContentCategory contentCategory : contentCategories) {
                EasyUITreeNode node = new EasyUITreeNode();
                node.setId(contentCategory.getId());
                node.setText(contentCategory.getName());
                node.setState(contentCategory.getIsParent()?"closed":"open");
                nodes.add(node);
            }
        }

        return nodes;
    }


    //创建节点
    public TaotaoResult addContentCategory(Long parentId, String name) {
        //补全属性
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setIsParent(false);
        contentCategory.setParentId(parentId);
        contentCategory.setName(name);
        //状态 1正常 2删除
        contentCategory.setStatus(1);
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        //需要主键返回
        contentCategoryMapper.insertSelectiveCallBackId(contentCategory);
        //判断父节点isParent
        ContentCategory category = contentCategoryMapper.selectByPrimaryKey(parentId);
        if(!category.getIsParent()) {
            category.setIsParent(true);
            //更新contentCategory;
            contentCategoryMapper.updateByPrimaryKeySelective(category);
        }
        //返回result
        return TaotaoResult.ok(contentCategory);
    }


}
